home *** CD-ROM | disk | FTP | other *** search
Text File | 1993-11-10 | 9.5 KB | 400 lines | [TEXT/KAHL] |
- /******************************************************************************
- GNUEditDoc.c
-
- Document methods for a tiny editor.
-
- Copyright © 1989 Symantec Corporation. All rights reserved.
-
- ******************************************************************************/
-
- #include <Global.h>
- #include <Commands.h>
- #include <CApplication.h>
- #include <CBartender.h>
- #include <CDataFile.h>
- #include <CDecorator.h>
- #include <CDesktop.h>
- #include <CError.h>
- #include <CPanorama.h>
- #include <CScrollPane.h>
- #include <TBUtilities.h>
- #include <CEditText.h>
- #include <CBureaucrat.h>
- #include <CWindow.h>
- #include <CEditPane.h>
- #include "GNUEditDoc.h"
- #include "GNUEditApp.h"
- #include "GNUStaticPane.h"
-
- #define WINDculture 500 /* Resource ID for WIND template */
-
- extern CApplication *gApplication; /* The application */
- extern CBartender *gBartender; /* The menu handling object */
- extern CDecorator *gDecorator; /* Window dressing object */
- extern CDesktop *gDesktop; /* The enclosure for all windows */
- extern CBureaucrat *gGopher; /* The current boss in the chain of command */
- extern OSType gSignature; /* The application's signature */
- extern CError *gError; /* The error handling object */
- extern CursHandle gWatchCursor; /* Watch cursor for waiting */
-
- /***
- * IGNUEditDoc
- *
- * This is your document's initialization method.
- * If your document has its own instance variables, initialize
- * them here.
- * The least you need to do is invoke the default method.
- *
- ***/
-
- void GNUEditDoc::IGNUEditDoc(CApplication *aSupervisor, Boolean printable)
-
- {
- CDocument::IDocument(aSupervisor, printable);
- non_static = true;
- }
-
-
- /***
- * NewFile
- *
- * When the user chooses New from the File menu, the CreateDocument()
- * method in your Application class will send a newly created document
- * this message. This method needs to create a new window, ready to
- * work on a new document.
- *
- * Since this method and the OpenFile() method share the code for creating
- * the window, you should use an auxiliary window-building method.
- *
- ***/
- void GNUEditDoc::NewFile(void)
- {
- NewFilefromHandle(NULL);
- }
-
- void GNUEditDoc::NewFilefromHandle(Handle theData)
- {
- CScrollPane *theScrollPane;
- Rect margin;
-
- non_static = true;
-
- if (theData)
- {
- Size theSize = GetHandleSize(theData);
- Ptr thePtr = *theData;
- if (theSize >= kMaxTELength)
- non_static = false;
-
- while (theSize--)
- {
- if (*thePtr == '\n') *thePtr = '\r';
- thePtr++;
- }
- }
-
- itsWindow = new(CWindow);
- itsWindow->IWindow(WINDculture, FALSE, gDesktop, this);
-
- theScrollPane = new(CScrollPane);
-
- theScrollPane->IScrollPane(itsWindow, this, 10, 10, 0, 0,
- sizELASTIC, sizELASTIC,
- TRUE, TRUE, TRUE);
-
- theScrollPane->FitToEnclFrame(TRUE, TRUE);
-
- if (non_static)
- {
- TEHandle macTE;
- CEditPane *theMainPane = new(CEditPane);
- theMainPane->IEditPane(theScrollPane, this);
- theMainPane->SetFontNumber(monaco);
- theMainPane->SetFontSize(9);
- theMainPane->SetWholeLines(true);
- macTE = ((CEditText *)theMainPane)->macTE;
- (**macTE).crOnly = -1;
- theMainPane->Scroll(-2,-2,false);
- itsMainPane = theMainPane;
- itsGopher = theMainPane;
- }
- else
- {
- GNUStaticPane *theMainPane = new(GNUStaticPane);
- theMainPane->IGNUStaticPane(theScrollPane, this);
- itsMainPane = theMainPane;
- itsGopher = theMainPane;
- }
-
- theScrollPane->InstallPanorama((CPanorama *)itsMainPane);
-
- if (theData)
- ((CAbstractText *)itsMainPane)->SetTextHandle(theData);
-
- gDecorator->PlaceNewWindow(itsWindow);
-
- DisposHandle(theData);
-
- itsWindow->Select();
- }
-
- /***
- * OpenFile
- *
- * When the user chooses Open… from the File menu, the OpenDocument()
- * method in your Application class will let the user choose a file
- * and then send a newly created document this message. The information
- * about the file is in the SFReply record.
- *
- * In this method, you need to open the file and display its contents
- * in a window. This method uses the auxiliary window-building method.
- *
- ***/
-
- void GNUEditDoc::OpenFile(SFReply *macSFReply)
-
- {
- CDataFile *theFile;
- Handle theData;
- Str63 theName;
- OSErr theError;
-
- /**
- ** Create a file and send it a SFSpecify()
- ** message to set up the name, volume, and
- ** directory.
- **
- **/
-
- theFile = new(CDataFile);
-
- /**
- ** Be sure to set the instance variable
- ** so other methods can use the file if they
- ** need to. This is especially important if
- ** you leave the file open in this method.
- ** If you close the file after reading it, you
- ** should be sure to set itsFile to NULL.
- **
- **/
-
- itsFile = theFile;
-
- theFile->IDataFile();
- theFile->SFSpecify(macSFReply);
-
-
- /**
- ** Send the file an Open() message to
- ** open it. You can use the ReadSome() or
- ** ReadAll() methods to get the contents of the file.
- **
- **/
-
- theFile->Open(fsRdWrPerm);
-
- theData = theFile->ReadAll(); /* ReadAll() creates the handle */
-
- NewFilefromHandle(theData);
-
- /**
- ** In this implementation, we leave the file
- ** open. You might want to close it after
- ** you've read in all the data.
- **
- **/
-
- itsFile->GetName(theName);
- itsWindow->SetTitle(theName);
-
- if (!non_static)
- {
- itsFile->Close();
- itsFile->Dispose();
- itsFile = NULL;
- }
-
- }
-
-
-
- /***
- * DoSave
- *
- * This method handles what happens when the user chooses Save from the
- * File menu. This method should return TRUE if the file save was successful.
- * If there is no file associated with the document, you should send a
- * DoSaveFileAs() message.
- *
- ***/
-
- Boolean GNUEditDoc::DoSave(void)
-
- {
- Handle theData;
-
- if (itsFile == NULL)
- return(DoSaveFileAs());
- else {
- theData = ((CAbstractText *)itsMainPane)->GetTextHandle();
- ((CDataFile *)itsFile)->WriteAll(theData);
- dirty = FALSE; /* Document is no longer dirty */
- gBartender->DisableCmd(cmdSave);
- return(TRUE); /* Save was successful */
- }
- }
-
-
- /***
- * DoSaveAs
- *
- * This method handles what happens when the user chooses Save As… from
- * File menu. The default DoCommand() method for documents sends a DoSaveFileAs()
- * message which displays a standard put file dialog and sends this message.
- * The SFReply record contains all the information about the file you're about
- * to create.
- *
- ***/
-
- Boolean GNUEditDoc::DoSaveAs(SFReply *macSFReply)
-
- {
- /**
- ** If there's a file associated with this document
- ** already, close it. The Dispose() method for files
- ** sends a Close() message to the file before releasing
- ** its memory.
- **
- **/
-
- if (itsFile != NULL)
- itsFile->Dispose();
-
-
- /**
- ** Create a new file, and then save it normally.
- **
- **/
-
- itsFile = new(CDataFile);
- ((CDataFile *)itsFile)->IDataFile();
- itsFile->SFSpecify(macSFReply);
- if (itsFile->ExistsOnDisk()) itsFile->ThrowOut();
- itsFile->CreateNew(gSignature, 'TEXT');
- itsFile->Open(fsRdWrPerm);
-
- itsWindow->SetTitle(macSFReply->fName);
-
- return( DoSave() );
- }
-
- /******************************************************************************
- DoCommand {OVERRIDE}
-
- Execute a command
- ******************************************************************************/
-
- void GNUEditDoc::DoCommand(long theCommand)
- {
- switch (theCommand) {
- case cmdCPP: case cmdCC1: case cmdCC1MPW: case cmdAS:
- {
- int argc = 0;
- char *argv[500];
- unsigned char *ptr;
- CWindow *newwin;
- Str255 theInput,theOutput;
- void munge(int, char **, int (*)(int,char **), Handle, short optstr);
- Handle stdin = ((CAbstractText *)itsMainPane)->GetTextHandle();
- itsWindow->GetTitle(theInput);
- theInput[1+*theInput] = 0;
- BlockMove(theInput, theOutput, 2+*theInput);
- ptr = &theOutput[*theOutput];
- while ((ptr > theOutput) && (*ptr != '.')) --ptr;
- if (ptr == theOutput) ptr = &theOutput[1+*theOutput];
- switch(theCommand)
- {
- case cmdCPP: // "-undef"
- {
- int cpp_main(int, char **);
- BlockMove(".i", ptr, 3);
- argv[argc++] = "cpp";
- argv[argc++] = (char *)&theInput[1];
- argv[argc++] = (char *)&theOutput[1];
- munge(argc,argv,cpp_main,stdin,theCommand);
- break;
- }
- case cmdCC1: // "-quiet","-mgas","-mintlib","-mnodispatch"
- {
- int cc1_main(int, char **);
- argv[argc++] = "cc1";
- argv[argc++] = "-mgas";
- argv[argc++] = (char *)&theInput[1];
- munge(argc,argv,cc1_main,stdin,theCommand);
- break;
- }
- case cmdCC1MPW: // "-quiet","-mintlib","-mnodispatch"
- {
- int cc1_main(int, char **);
- argv[argc++] = "cc1";
- argv[argc++] = (char *)&theInput[1];
- munge(argc,argv,cc1_main,stdin,theCommand);
- break;
- }
- case cmdAS: // "-m68000"
- {
- int as_main(int, char **);
- argv[argc++] = "gas";
- argv[argc++] = "-o";
- BlockMove(".o", ptr, 3);
- argv[argc++] = (char *)&theOutput[1];
- argv[argc++] = (char *)&theInput[1];
- munge(argc,argv,as_main,stdin,theCommand);
- break;
- }
- }
- }
-
- break;
- #if 0
- case cmdlaunch:
- {
- SFReply macSFReply;
- Str63 theName;
- OSErr theError;
- CFile *theFile;
- Point corner; /* Top left corner of dialog box */
- Boolean wasLocked;
- OSType sfFileTypes[] = {'APPL'};
- /* Center dialog box on the screen */
- FindDlogPosition('DLOG', getDlgID, &corner);
-
- wasLocked = Lock( TRUE);
-
- SFPGetFile(corner, "\p", NULL, 1, sfFileTypes,
- NULL, &macSFReply, getDlgID, NULL);
-
- Lock( wasLocked);
- if (macSFReply.good)
- {
- SetCursor(*gWatchCursor);
- theFile = new(CFile);
- theFile->IFile();
- theFile->SFSpecify(&macSFReply);
- }
- }
- break;
- #endif
- default:
- inherited::DoCommand(theCommand);
- break;
- }
- }
-
- void GNUEditDoc::Notify(CTask *theTask)
- {
- NotifyClean(theTask);
- dirty = non_static;
- }
-
-